package cn.itrip.trade.controller;

import cn.itrip.beans.dto.Dto;
import cn.itrip.common.DtoUtils;
import cn.itrip.trade.service.ItripHotelOrderPayService;
import org.apache.commons.io.IOUtils;

import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;

/**
 * Created by Administrator on 2018/12/3.
 */
@RestController
@RequestMapping("/api")
public class WxController {
    Logger logger  =  Logger.getLogger(WxController.class);
    @Resource
    private ItripHotelOrderPayService itripHotelOrderPayService;

    //微信支付
    @RequestMapping( value = "/wxpay/createqccode/{orderNo}",method = RequestMethod.GET)
    public Dto createqccode(@PathVariable("orderNo") String orderNo){
        return  itripHotelOrderPayService.updateOrderByorderNo(orderNo);
    }
    //微信回调
    @RequestMapping("/jieguo")
    public  void jieguo(HttpServletRequest request,HttpServletResponse response){
        InputStream inputStream= null;
        try {
            inputStream = request.getInputStream();
            String xmlResult= IOUtils.toString(inputStream,"utf-8");
            boolean flag= itripHotelOrderPayService.huidiao(xmlResult);
            if (flag){
                String msgToWx="<xml> \n" +
                        "\n" +
                        "  <return_code><![CDATA[SUCCESS]]></return_code>\n" +
                        "  <return_msg><![CDATA[OK]]></return_msg>\n" +
                        "</xml> \n";
                response.getWriter().write(msgToWx);
                response.getWriter().flush();
            }
        } catch (Exception e) {
            //打印异常信息
            logger.debug("出错了"+e);
        }
    }
    @RequestMapping(value = "/wxpay/queryorderstatus/{orderNo}",method = RequestMethod.GET)
    public  Dto queryorderstatus(@PathVariable("orderNo") String orderNo){
        return itripHotelOrderPayService.RenderResponse(orderNo);
    }
}
